package org.atf.model.dao;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.atf.model.entity.Audit;
import org.atf.model.entity.Preference;
import org.atf.model.utils.EntityManagerUtil;

public class AuditDAO {
	
	public List<Audit> getAllAudit(Integer maxresults){	
		EntityManager entityManager=EntityManagerUtil.getEntityManager();
		Query query = entityManager.createQuery("select a from Audit a ");
		query.setMaxResults(maxresults);
		List<Audit> resultList = query.getResultList();
		return resultList;
	}
	
	public void insertAudit(Audit audit){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		try{
			entityManager.getTransaction().begin();
			entityManager.persist(audit);
			entityManager.getTransaction().commit();
		}finally {
		//	entityManager.close();
		}
	}
	public void deleteAllByServerId(Integer serverId){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		try{
			entityManager.getTransaction().begin();
/// TO DO Delete by severId			Query query = entityManager.createQuery()
			entityManager.flush();
			entityManager.getTransaction().commit();
		}finally {
		//	entityManager.close();
		}
	}
	public void deleteAllByProfileId(Integer profileId){
		
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		try{
			entityManager.getTransaction().begin();
/// TO DO Delete by Profilei			Query query = entityManager.createQuery()
			entityManager.flush();
			entityManager.getTransaction().commit();
		}finally {
		//	entityManager.close();
		}
	}
	
	 public List<Audit> getAllAudit(int startIndex, int pageSize) {
		 EntityManager entityManager = EntityManagerUtil.getEntityManager();
		 List<Audit> audits = new ArrayList<Audit>();
			try{
				entityManager.getTransaction().begin();
				TypedQuery<Audit> query = entityManager.createQuery(
				           "SELECT a FROM Audit a ", Audit.class);
				query.setFirstResult(startIndex);
			    query.setMaxResults(pageSize);
			    audits=query.getResultList();
				entityManager.getTransaction().commit();
			}finally {
			//	entityManager.close();
			}
		 
		 return audits;    
	      
	   }
	 public List<Audit> getAudits(Integer serverId, int maxResults){
			EntityManager entityManager=EntityManagerUtil.getEntityManager();
			Query query = entityManager.createQuery("select a from Audit a where a.serverId = :serverId order by a.gentime desc");
			
			query.setParameter("serverId", serverId);
			query.setMaxResults(maxResults);
			List<Audit> resultList = query.getResultList();
			return resultList;
	 }
	 public List<Audit> getAllAuditByServer(int serverId,int startIndex, int pageSize) {
		 EntityManager entityManager = EntityManagerUtil.getEntityManager();
		 List<Audit> audits = new ArrayList<Audit>();
			try{
				entityManager.getTransaction().begin();
				TypedQuery<Audit> query = entityManager.createQuery(
				           "SELECT a FROM Audit a where a.serverId =:serverId", Audit.class);
				query.setParameter("serverId", serverId);
				query.setFirstResult(startIndex);
			    query.setMaxResults(pageSize);
			    audits=query.getResultList();
				entityManager.getTransaction().commit();
			}finally {
			//	entityManager.close();
			}
		 
		 return audits;    
	      
	   }

	public List<Audit> getAllAuditFiltered(Integer maxResult,
			HashMap<String, String> filterObj) {
		// TODO Auto-generated method stub
		String sql="select a from Audit a";
		System.out.println(filterObj);
		if(filterObj != null){
			if( !filterObj.isEmpty()){						
				sql+=" where";
				boolean addAnd=false;
				if(filterObj.containsKey("username")){
					sql+=" a.username= :username";
					addAnd=true;
				}
				if(filterObj.containsKey("task")){
					if(addAnd){
						sql+=" and";
					}
					sql+=" a.task= :task";
					addAnd=true;
				}
				if(filterObj.containsKey("fromTime") &&  
						filterObj.containsKey("toTime") ){
					if( addAnd){
						sql+=" and";
					}
					sql+=" a.gentime between :fromTime and  :toTime";
				}else if( filterObj.containsKey("fromTime") ){
					if( addAnd){
						sql+=" and";
					}
					sql+=" a.gentime > :fromTime";
					
				}else if (filterObj.containsKey("toTime")){
					if( addAnd){
						sql+=" and";
					}
					sql+=" a.gentime < :toTime";
				}
				
				
			}
		}
		sql+=" order by a.gentime DESC";
		System.out.println(sql);
		EntityManager entityManager=EntityManagerUtil.getEntityManager();
		Query query = entityManager.createQuery(sql);
		if(filterObj.containsKey("username")){
			query.setParameter("username", filterObj.get("username"));
			
		}
		if(filterObj.containsKey("task")){
			query.setParameter("task", filterObj.get("task"));
			
		}
		if(filterObj.containsKey("fromTime")){
			Date fromDate;
			try {
				fromDate = (Date) new SimpleDateFormat("MM/dd/yyyy h:mm a").parse(filterObj.get("fromTime"));
				query.setParameter("fromTime", fromDate);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		if(filterObj.containsKey("toTime")){
			Date toDate;
			try {
				toDate = (Date) new SimpleDateFormat("MM/dd/yyyy h:mm a").parse(filterObj.get("toTime"));
				query.setParameter("toTime", toDate);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		query.setMaxResults(maxResult);
		List<Audit> resultList = query.getResultList();
		return resultList;
	}
}
